import axios from "axios";
import { HWMI_URLS } from "../../modules/Api";

import { ref,watch } from "vue";

export default function ({ emit }, { successEvent,errorEvent }) {
    // 当前展示的实体对象，用于功能0和1
    const currentEntity = ref({
        entityName: "",
        type: "",
        properties: []
    });

    // 自动拆分功能
    watch(() => currentEntity.value.entityName, (n, o) => {
        const splitted = n.split("..");
        if (splitted.length >= 2 && splitted[0] !== "" && splitted[1] != "") {
            currentEntity.value.entityName = splitted[0];
            currentEntity.value.type = splitted.slice(1).join("");
        }
    });

    // 清除当前实体对象
    const clearCurrentEntity = () => {
        currentEntity.value = {
            entityName: "",
            type: "",
            properties: []
        };
    };

    // 实体操作按钮的加载中图标是否显示
    const isEntityLoadingShow = ref(false);

    // 添加实体
    const addEntity = () => {
        if (currentEntity.value.entityName === ""
            || currentEntity.value.type === ""
            || currentEntity.value.properties.some(x => x.name === "" || x.text === "")) {
            emit(errorEvent, "请输入所有字段");
            return;
        }

        isEntityLoadingShow.value = true;

        axios.post(HWMI_URLS.managementUrls.graphDbManagementUrls.addEntityUrl,
            currentEntity.value)
            .then(res => {
                if (res.data.success) {
                    emit(successEvent, "成功新增实体");
                }
                else {
                    emit(errorEvent, res.data.message);
                }
            })
            .catch(reason => {
                console.log(reason);
                emit(errorEvent, reason);
            })
            .finally(() => {
                isEntityLoadingShow.value = false;
            });
    };

    // 查询实体
    let entityOrigName = ref("");
    let entityOrigType = ref("");
    let isEntityQueried = ref(false);
    const queryEntity = () => {
        if (currentEntity.value.entityName === "" || currentEntity.value.type === "") {
            emit(errorEvent, "请先输入要修改实体的名称和类型进行查询");
            return;
        }

        isEntityLoadingShow.value = true;

        axios.post(HWMI_URLS.managementUrls.graphDbManagementUrls.queryEntityUrl, {
            entityName: currentEntity.value.entityName,
            type:currentEntity.value.type
        })
            .then(res => {
                if (res.data.success) {
                    currentEntity.value.entityName = res.data.label;
                    currentEntity.value.type = res.data.type;
                    currentEntity.value.properties = res.data.properties;

                    entityOrigName.value = res.data.label;
                    entityOrigType.value = res.data.type;
                    isEntityQueried.value = true;
                }
                else {
                    emit(errorEvent, res.data.message);
                }
            })
            .catch(reason => {
                console.log(reason);
                emit(errorEvent, reason);
            })
            .finally(() => {
                isEntityLoadingShow.value = false;
            });
    };

    //修改实体
    const modifyEntity = () => {
        if (currentEntity.value.entityName === ""
            || currentEntity.value.type === ""
            || currentEntity.value.properties.some(x => x.name === "" || x.text === "")) {
            emit(errorEvent, "请输入所有字段");
            return;
        }

        isEntityLoadingShow.value = true;

        axios.post(HWMI_URLS.managementUrls.graphDbManagementUrls.modifyEntityUrl,
            {
                entityOrigName: entityOrigName.value,
                entityOrigType: entityOrigType.value,
                ...currentEntity.value
            })
            .then(res => {
                if (res.data.success) {
                    emit(successEvent, "成功修改实体");
                }
                else {
                    emit(errorEvent, res.data.message);
                }
            })
            .catch(reason => {
                console.log(reason);
                emit(errorEvent, reason);
            })
            .finally(() => {
                isEntityLoadingShow.value = false;
            });
    };


    // 删除实体
    let entityToRemoveName = ref("");
    let entityToRemoveType = ref("");

    // 自动拆分功能
    watch(entityToRemoveName, (n, o) => {
        const splitted = n.split("..");
        if (splitted.length >= 2 && splitted[0] !== "" && splitted[1] != "") {
            entityToRemoveName.value = splitted[0];
            entityToRemoveType.value = splitted.slice(1).join("");
        }
    });

    const removeEntity = () => {
        if (entityToRemoveName.value === ""||entityToRemoveType.value==="") {
            emit(errorEvent, "请输入要删除实体的名称和类型");
            return;
        }

        isEntityLoadingShow.value = true;

        axios.post(HWMI_URLS.managementUrls.graphDbManagementUrls.removeEntityUrl,
            { entityName: entityToRemoveName.value,type:entityToRemoveType.value })
            .then(res => {
                if (res.data.success) {
                    emit(successEvent, `成功删除实体【${entityToRemoveName.value}】`);
                }
                else {
                    emit(errorEvent, res.data.message);
                }
            })
            .catch(reason => {
                console.log(reason);
                emit(errorEvent, reason);
            })
            .finally(() => {
                isEntityLoadingShow.value = false;
            });
    };

    return {
        currentEntity,
        clearCurrentEntity,

        isEntityLoadingShow,

        addEntity,

        isEntityQueried,
        entityOrigName,
        queryEntity,
        modifyEntity,

        entityToRemoveName,
        entityToRemoveType,
        removeEntity
    };
}